package com.yang.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpRequestUtil {

	private static final Logger log = LoggerFactory.getLogger(HttpRequestUtil.class);

	public static String get(String urlStr) {
		for (int i = 0; i < 3; i++) {
			HttpURLConnection httpURLConnection = null;
			try {
				URL url = new URL(urlStr);
				httpURLConnection = (HttpURLConnection) url.openConnection();
				httpURLConnection.setRequestMethod("GET");
				httpURLConnection.setDoInput(true);
				httpURLConnection.setDoOutput(true);
				httpURLConnection.connect();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(httpURLConnection.getInputStream(), "UTF8"));
				String result = "";
				String line = null;
				while ((line = reader.readLine()) != null) {
					result = result + line;
				}
				reader.close();
				return result;
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			} finally {
				if (httpURLConnection != null) {
					httpURLConnection.disconnect();
				}
			}
		}
		return null;
	}

	public static String post(String urlstr, String param) {
		for (int i = 0; i < 3; i++) {
			HttpURLConnection httpURLConnection = null;
			try {
				URL url = new URL(urlstr);
				httpURLConnection = (HttpURLConnection) url.openConnection();
				httpURLConnection.setRequestMethod("POST");
				httpURLConnection.setDoOutput(true);
				httpURLConnection.setDoInput(true);
				// 参数
				/*
				PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
				printWriter.write(param);
				printWriter.flush();*/
				OutputStream outputStream = httpURLConnection.getOutputStream();
				outputStream.write(param.getBytes("UTF-8"));
				outputStream.flush();
				// 请求
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(httpURLConnection.getInputStream(), "UTF8"));
				String result = "";
				String line = null;
				while ((line = reader.readLine()) != null) {
					result = result + line;
				}
				//printWriter.close();
				outputStream.close();
				reader.close();
				return result;
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			} finally {
				if (httpURLConnection != null) {
					httpURLConnection.disconnect();
				}
			}
		}
		return null;
	}

	public static String postWithHeader(String urlstr, Map<String, String> header, String param) {
		for (int i = 0; i < 3; i++) {
			HttpURLConnection httpURLConnection = null;
			try {
				URL url = new URL(urlstr);
				httpURLConnection = (HttpURLConnection) url.openConnection();
				httpURLConnection.setRequestMethod("POST");
				httpURLConnection.setDoOutput(true);
				httpURLConnection.setDoInput(true);
				// 请求头
				for (Entry<String, String> entry : header.entrySet()) {
					httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
				}
				// 参数
				/*
				PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
				printWriter.write(param);
				printWriter.flush();*/
				OutputStream outputStream = httpURLConnection.getOutputStream();
				outputStream.write(param.getBytes("UTF-8"));
				outputStream.flush();
				// 请求
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(httpURLConnection.getInputStream(), "UTF8"));
				String result = "";
				String line = null;
				while ((line = reader.readLine()) != null) {
					result = result + line;
				}
				//printWriter.close();
				outputStream.close();
				reader.close();
				return result;
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			} finally {
				if (httpURLConnection != null) {
					httpURLConnection.disconnect();
				}
			}
		}
		return null;
	}

}
